package com.dreamwhale.machinery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.entity.YxUserLevel;
import com.dreamwhale.machinery.dao.YxUserLevelMapper;
import com.dreamwhale.machinery.service.YxUserLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户等级记录表 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-07-01
 */
@Service
public class YxUserLevelServiceImpl extends ServiceImpl<YxUserLevelMapper, YxUserLevel> implements YxUserLevelService {

    @Override
    public Map inquiryVipInfo(Integer userId) {
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        List<YxUserLevel> list = this.list(new QueryWrapper<YxUserLevel>().eq("uid", userId).orderByDesc("add_time"));
        if (!CollectionUtils.isEmpty(list)) {
            YxUserLevel yxUserLevel = list.get(0);
            LocalDateTime validTime = yxUserLevel.getValidTime();
            if (LocalDateTime.now().compareTo(validTime) > 0) {
                stringObjectHashMap.put("isVip", true);
                stringObjectHashMap.put("vipLevel", yxUserLevel.getLevelId());
                stringObjectHashMap.put("validTime", validTime);
                return stringObjectHashMap;
            }
        }
        stringObjectHashMap.put("isVip", false);
        stringObjectHashMap.put("vipLevel", 0);
        stringObjectHashMap.put("validTime", null);
        return stringObjectHashMap;
    }
}
